package com.tianqu.tms.www.system.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.AucBidType;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.system.viewhelper.AucBidTypeManageViewHelper;

/**
 * @author zhangxueliang 竞价模式添加结果
 */
public class AucBidTypeCreationResultController extends TmsController {
	private Logger logger = Logger
			.getLogger(AucBidTypeCreationResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			AucBidType aucBidType = AucBidType.getInstance();
			Codes codes = Codes.getInstance();
			AucBidTypeManageViewHelper viewHelper = new AucBidTypeManageViewHelper();
			viewHelper = (AucBidTypeManageViewHelper) uc
					.getAttribute(TmsConstants.VIEWHELP);
			aucBidType.setTypeName(viewHelper.getTypeName());
			aucBidType.setFareAmount(Integer.parseInt(viewHelper
					.getFareAmount()));
			aucBidType.setAuctionDeadline(Integer.parseInt(viewHelper
					.getAuctionDeadline()));
			aucBidType
					.setDelayTime(Integer.parseInt(viewHelper.getDelayTime()));
			aucBidType.setFirstExpetPrices(viewHelper.getFirstExpetPrices());
			aucBidType.setBidType(codes.getItemCodeByItemIdAndItemValue(
					Codes.AUCTION_TYPR, viewHelper.getTypeName()));
			TmsUserProfile profile = uc.getUserProfile();
			aucBidType.setUpdateUser(profile.getUniqueId());

			aucBidType.insertAucBidType(aucBidType);

			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
